package org.kmp.project

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.togetherWith
import androidx.compose.animation.with
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.kmp.project.main.Main

import org.kmp.project.util.ToasterProvider

@Composable
@Preview
fun App() {

    MaterialTheme {
        ToasterProvider {

            if (NavigationManager.isElementsEmpty()) {
                NavigationManager.navigateTo(Main())
            }

            Box(Modifier
                .fillMaxSize()
                .systemBarsPadding()
            ) {
                if (!NavigationManager.isElementsEmpty()) {
                    val currentEntry =NavigationManager.currentElement()

                    AnimatedContent(
                        targetState = currentEntry,
                        transitionSpec = {
                            if (NavigationManager.direction == NavDirection.Forward) {
                                slideInHorizontally(
                                    animationSpec = tween(300),
                                    initialOffsetX = { fullWidth -> fullWidth }  // 从右进
                                ) + fadeIn() togetherWith
                                        slideOutHorizontally(
                                            animationSpec = tween(300),
                                            targetOffsetX = { fullWidth -> -fullWidth }  // 往左出
                                        ) + fadeOut()
                            } else {
                                slideInHorizontally(
                                    animationSpec = tween(300),
                                    initialOffsetX = { fullWidth -> -fullWidth } // 从左进
                                ) + fadeIn() togetherWith
                                        slideOutHorizontally(
                                            animationSpec = tween(300),
                                            targetOffsetX = { fullWidth -> fullWidth }   // 往右出
                                        ) + fadeOut()
                            }
                        }
                    ) { entry ->
                        entry?.screen()
                    }
                }
            }
        }
    }
}